﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using PMS.BLL;
using PMS.Entity;
using PMS;
using PMS.Resources;
using PMS.Win;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Views.Base;
using System.Collections;
using PMS.Utils;

namespace PMS.SYS
{
    public partial class AccountLookup : FormPopupBase
    {
        private readonly AccountBO service = new AccountBO();
        public Account AccountSelected;
        private List<ComboItem> m_RoleList;

        public AccountLookup()
        {
            InitializeComponent();
            InitStyle();
            InitData();
        }

      
        void InitStyle()
        {
            grvAccountList.OptionsBehavior.Editable = false;
            grvAccountList.OptionsView.ShowGroupPanel = false;
            grvAccountList.OptionsView.ShowIndicator = true;

            grvAccountList.OptionsView.ShowAutoFilterRow = true;
            grvAccountList.OptionsView.ShowFilterPanelMode = ShowFilterPanelMode.Never;
            grvAccountList.OptionsView.ShowGroupedColumns = false;
            grvAccountList.OptionsMenu.EnableColumnMenu = false;
            grvAccountList.OptionsCustomization.AllowColumnMoving = false;
            grvAccountList.OptionsCustomization.AllowGroup = false;
            grvAccountList.OptionsFilter.AllowFilterEditor = false;

            this.grvAccountList.OptionsSelection.MultiSelect = false;
            this.grvAccountList.IndicatorWidth = Constants.INDICATOR_WIDTH;
            //gridbinding
            this.colID.FieldName = AccountColumns.ID.ToString();
            this.colFullname.FieldName = AccountColumns.Hoten.ToString();
            this.ColAddress.FieldName = AccountColumns.Diachi.ToString();
            this.colPhone.FieldName = AccountColumns.SoDT.ToString();
            this.colChucVu.FieldName = AccountColumns.Chucvu.ToString();
            this.colEmail.FieldName = AccountColumns.Email.ToString();
            this.colRole.UnboundType = DevExpress.Data.UnboundColumnType.String;
        }

        void InitData()
        {
            LoadRoleCbbData();
            try
            {
                this.grdAccountList.DataSource = service.GetAllAccount(); 
            }
            catch (Exception ex)
            {
                ShowErrorMessage(ex);
            }

        }

        private void LoadRoleCbbData()
        {
            ArrayList tempItemList = BindDataUtil.BindDataCombobox("AccountRole", typeof(AccountRole));
            m_RoleList = new List<ComboItem>();
            foreach (ComboItem item in tempItemList)
            {
                m_RoleList.Add(item);
            }
            cbbColRole.Items.AddRange(m_RoleList);
        }

        private void CloseDialog(DialogResult result)
        {
            try
            {
                if (result == DialogResult.OK)
                {
                    this.AccountSelected = (Account)grvAccountList.GetFocusedRow();
                }
                else
                {
                    this.AccountSelected = null;
                }

                this.DialogResult = result;
            }
            catch (Exception ex)
            {
                ShowErrorMessage(ex);
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            CloseDialog(DialogResult.OK);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            CloseDialog(DialogResult.Cancel);
        }

        private void grvAccountList_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if ((e.Info.IsRowIndicator) && (e.RowHandle >= 0))
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }

        private void grvAccountList_DoubleClick(object sender, EventArgs e)
        {
            GridView view = sender as GridView;
            GridHitInfo hi = view.CalcHitInfo(grdAccountList.PointToClient(MousePosition));
            if (hi.InRow || hi.InRowCell)
            {
                if (AccountSelected != null)
                {
                    CloseDialog(DialogResult.OK);
                }
            }
        }

        private void grvAccountList_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                btnOk_Click(sender, null);
            }
            else if (e.KeyCode == Keys.Escape)
            {
                btnCancel_Click(sender, null);
            }
        }

        private void grvAccountList_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            AccountSelected = this.grvAccountList.GetFocusedRow() as Account;
        }

        private void grvAccountList_ShowingEditor(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
        }

        private void grvAccountList_CustomUnboundColumnData(object sender, CustomColumnDataEventArgs e)
        {
            Account tempRow = (Account)this.grvAccountList.GetRow(e.RowHandle);
            if ((e.Column == colRole) && e.IsGetData && e.RowHandle >= 0)
            {
                foreach (ComboItem item in m_RoleList)
                {
                    if (tempRow.Role == (byte)item.ItemValue)
                    {
                        e.Value = item.DisplayValue;
                        break;
                    }
                }

            }
        }
    }
}
